package com.bdqn;

import com.bdqn.pojo.GenConfig;
import com.bdqn.service.SysGeneratorService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 使用SpringBootTest生成项目代码
 *
 * @author lilibo
 * @date 2019/2/1
 */
@SpringBootTest
public class GeneratorTester {

    @Resource
    private SysGeneratorService sysGeneratorService;

    /**
     * 生成项目（或模块）的代码
     */
    @Test
    public void testGeneratorCode() {
        System.out.println("----- Generator Code ------");
        GenConfig genConfig = new GenConfig();
        genConfig.setProjectPath("D:/Works/boboworkspace/studyworkspace/qniao/qniao-module/qniao-demo"); // 代码生成路径
        genConfig.setPackageName("com.bdqn"); // 包名（不能为空）
        genConfig.setModuleName("demo"); // 模块名（不能为空）
        genConfig.setTablePrefix(""); // 表前缀（可为空）
        genConfig.setTableName("student"); // 表名（不能为空）
        boolean ok = sysGeneratorService.generatorCodeLocal(genConfig);
        System.out.println("\n" + genConfig.toString() + "\n\n" + (ok ? "SUCCESS" : "FAILING"));
        assert ok;
    }

    /**
     * 批量生成项目（或模块）的代码
     */
    @Test
    public void testGeneratorCodeBatch() {
        System.out.println("----- Generator Code Batch ------");
        String projectPath = "D:/Works/boboworkspace/bifrost-project/bifrost-demo"; // 代码生成路径
        String packageName = "com.bdqn"; // 包名（不能为空）
        String moduleName = "demo"; // 模块名（不能为空）
        String tablePrefix = "t_"; // 表前缀（可为空）
        String[] tableNames = {"grade","subject","student","result"}; // {"sys_user","sys_role","sys_user_role","sys_dept","sys_role_dept","sys_menu","sys_role_menu","sys_oauth_client_details","sys_dept_relation","sys_dict","sys_log"};
        List<GenConfig> genConfigList = new ArrayList<>();
        GenConfig genConfig = new GenConfig();
        genConfig.setProjectPath(projectPath);
        genConfig.setPackageName(packageName);
        genConfig.setModuleName(moduleName);
        genConfig.setTablePrefix(tablePrefix);
        Arrays.stream(tableNames).forEach(tableName -> {
            genConfig.setTableName(tableName);
            genConfigList.add(genConfig);
            boolean ok = sysGeneratorService.generatorCodeLocal(genConfig);
            System.out.println("\n" + genConfig.toString() + "\n\n" + (ok ? "SUCCESS" : "FAILING"));
        });
        assert true;
    }

    /**
     * 批量生成多模块项目的子工程
     */
    @Test
    public void testGeneratorProjectCodeBatch() {
        System.out.println("----- Generator Project Code Batch ------");
        String projectPath = "D:/Works/boboworkspace/qniao/qniao-module/qniao-demo"; // 代码生成路径
        String packageName = "com.bdqn"; // 包名
        String moduleName = "demo"; // 模块名
        String tablePrefix = "t_"; // 表前缀
        String[] tableNames = {"sys_user","sys_role"}; // {"sys_user","sys_role","sys_user_role","sys_dept","sys_role_dept","sys_menu","sys_role_menu","sys_oauth_client_details","sys_dept_relation","sys_dict","sys_log"};
        GenConfig genConfig = new GenConfig();
        genConfig.setProjectPath(projectPath);
        genConfig.setPackageName(packageName);
        genConfig.setModuleName(moduleName);
        genConfig.setTablePrefix(tablePrefix);
        // 创建工程模块
        boolean pomok = sysGeneratorService.generatorProjectModule(genConfig);
        System.out.println("\n" + genConfig.toString() + "\n\n" + (pomok ? "SUCCESS" : "FAILING"));

        // 生成模块代码
        List<GenConfig> genConfigList = new ArrayList<>();
        Arrays.stream(tableNames).forEach(tableName -> {
            genConfig.setTableName(tableName);
            genConfigList.add(genConfig);
            boolean ok = sysGeneratorService.generatorCodeLocal(genConfig);
            System.out.println("\n" + genConfig.toString() + "\n\n" + (ok ? "SUCCESS" : "FAILING"));
        });
        assert true;
    }

    /**
     * 批量生成项目（或模块）的代码
     */
    @Test
    public void testGeneratorBifrostBatch() {
        System.out.println("----- Generator Code Batch ------");
        String projectPath = "D:/Works/boboworkspace/bifrost-project/bifrost-api"; // 代码生成路径
        String packageName = "com"; // 包名
        String moduleName = "bdqn"; // 模块名
        String tablePrefix = ""; // 表前缀
        String[] tableNames = {"shop_activity","shop_ad","shop_address","shop_attribute","shop_brand","shop_cart","shop_category","shop_collect","shop_comment","shop_feedback","shop_footprint","shop_goods","shop_goods_issue","shop_keywords","shop_order","shop_order_goods","shop_search_history","shop_sms_log","shop_user","shop_user_level"}; // {"sys_user","sys_role","sys_user_role","sys_dept","sys_role_dept","sys_menu","sys_role_menu","sys_oauth_client_details","sys_dept_relation","sys_dict","sys_log"};
        List<GenConfig> genConfigList = new ArrayList<>();
        GenConfig genConfig = new GenConfig();
        genConfig.setProjectPath(projectPath);
        genConfig.setPackageName(packageName);
        genConfig.setModuleName(moduleName);
        genConfig.setTablePrefix(tablePrefix);
        Arrays.stream(tableNames).forEach(tableName -> {
            genConfig.setTableName(tableName);
            genConfigList.add(genConfig);
            boolean ok = sysGeneratorService.generatorCodeLocal(genConfig);
            System.out.println("\n" + genConfig.toString() + "\n\n" + (ok ? "SUCCESS" : "FAILING"));
        });
        assert true;
    }

}